#include "manage.h"
#include <ctime>
#include <fstream>
#include <cstdlib>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::fstream;
using std::ios;

manage::manage()
{

}

//构造函数
manage::manage(const char fname[])
{
	char c_date[30], c_time[30];
	agenda temp;
	int status, total;
	this->db_fname = fname;

	this->before.reverse = false;									//设置每个双向循环链表的排序方向
	this->current.reverse = false;
	this->after.reverse = true;

	time_t t = time(0);												//获取当前时间
	strftime(c_date, sizeof(c_date), "%Y-%m-%d", localtime(&t));	//将当前时间按照格式生成字符串
	strftime(c_time, sizeof(c_time), "%H:%M:%S", localtime(&t));
	this->curTime.s_date = c_date;									//将当前时间存储到curTime日程中方便以后作比较
	this->curTime.s_time = c_time;

	fstream fp(fname, ios::in);
	fp >> total;
	for(int i = 1; i <= total; i++)									//读取所有日程
	{
		fp >> temp;
		status = get_status(temp);									//将日程进行分类并存储到相应的表中
		if(status == 0)
			this->before.ListInsert(temp);
		else if(status == 1)
			this->current.ListInsert(temp);
		else
			this->after.ListInsert(temp);
	}
	fp.close();
}

//日程分类函数
int manage::get_status(agenda obj)
{
	bool begin, end;

	if(this->curTime > obj)											//判断该日程是否开始
		begin = true;

	if(this->curTime.s_date < obj.e_date)							//判断该日程是否结束
		end = false;
	else if(this->curTime.s_date > obj.e_date)
		end = true;
	else
	{
		if(this->curTime.s_time < obj.e_time)
			end = false;
		else if(this->curTime.s_time > obj.e_time)
			end = true;
		else
			end = true;
	}

	if(begin && end)												//未开始
		return 0;
	else if(begin && !end)											//正在进行
		return 1;
	else															//已结束
		return 2;
}

//日程管理系统主界面
void manage::start()
{
	int op;
	system("title Agenda Managing System");
	while(1)
	{
		system("cls");
		cout << "1. Insert an agenda" << endl;
		cout << "2. Show all the agendas" << endl;
		cout << "3. Show the plans that is not begin" << endl;
		cout << "4. Show the ongoing plans" << endl;
		cout << "5. Show the plans that is already finished" << endl;
		cout << "6. Show the plans that will begin today" << endl;
		cout << "7. Show the plans that will begin this week" << endl;
		cout << "8. Show the plans that will begin this month" << endl;
		cout << "0. Exit" << endl;
		cout << "Your choice: ";
		cin >> op;
		cin.clear();
		if(op == 0)
			return;
		else if(op == 1)
			this->insert();
		else if(op == 2)
			this->show_schedule(-1);
		else if(op == 3)
			this->show_schedule(4);
		else if(op == 4)
			this->show_schedule(2);
		else if(op == 5)
			this->show_schedule(1); 
		else if(op == 6)
			this->show_beginning(1);
		else if(op == 7)
			this->show_beginning(2);
		else if(op == 8)
			this->show_beginning(3);
		else
		{
			cout << "Unknown operation.";
			cin.get();
		}
	}
	
}

//日程插入函数
void manage::insert()
{
	int status;
	agenda temp;

	cout << "Input the id of the agenda: ";							//获取日程信息
	cin >> temp.id;
	cout << "Input the start time of the agenda (YYYY-mm-dd HH:MM:SS): ";
	cin >> temp.s_date >> temp.s_time;
	cout << "Input the finish time of the agenda (YYYY-mm-dd HH:MM:SS): ";
	cin >> temp.e_date >> temp.e_time;
	cout << "Input the detail of the agenda: ";
	cin >> temp.detail;

	status = get_status(temp);										//将日程进行分类并插入表
	if(status == 0)
		this->before.ListInsert(temp);
	else if(status == 1)
		this->current.ListInsert(temp);
	else
		this->after.ListInsert(temp);

	cout << "Insert complete." << endl;
	system("pause");
}

//显示日程函数
void manage::show_schedule(int op)
{
	bool before, current, after;
	before = op&1;
	current = op&2;
	after = op&4;

	if(before)														//输出未开始日程表
	{
		cout << "The ended plans" << endl;
		this->before.Show();
		cout << endl;
	}
	if(current)														//输出正在进行日程表
	{	
		cout << "The ongoing plans" << endl;
		this->current.Show();
		cout << endl;
	}
	if(after)														//输出已结束日程表
	{
		cout << "The plans that is not stated" << endl;
		this->after.Show();
		cout << endl;
	}
	system("pause");
}

//将字符串转化成某一年的周数
int get_week(const char *str)
{
	time_t now;
	time(&now);
	struct tm tm_t = *localtime(&now);
	struct tm temp = tm_t;
	char cht[30];

	sscanf(str, "%d-%d-%d", &temp.tm_year,&temp.tm_mon,&temp.tm_mday);
	temp.tm_year -= 1900;
	temp.tm_mon -= 1;

	time_t ttt = mktime(&temp);
	strftime(cht, 20, "%W", localtime(&ttt));

	return atoi(cht)+1;
}

//输出当日即将开始的日程
void manage::show_beginning(int op)
{
	int len, i;
	agenda temp;
	system("cls");
	cout << "Current time: " << this->curTime.s_date << " " << this->curTime.s_time << endl;
	if(op == 1)
	{
		len = this->after.ListLength();
		cout << "The plans which is beginning today" << endl;
		for(i = 1; i <= len; i++)
		{
			temp = this->after.GetElem(i);
			if(temp.s_date == this->curTime.s_date)						//如果该日程的开始日期等于当前日期就将它输出
				cout << temp;
		}
		cout << endl;
	}
	if(op == 2)
	{
		int cur_c, temp_c;
		cur_c = get_week(this->curTime.s_date.c_str());
		len = this->after.ListLength();
		cout << "The plans which is beginning this week" << endl;
		for(i = 1; i <= len; i++)
		{
			temp = this->after.GetElem(i);
			temp_c = get_week(temp.s_date.c_str());
			if(temp_c == cur_c)										//如果该日程的开始周数等于当前周数就将它输出
				cout << temp;
		}
		cout << endl;
	}
	if(op == 3)
	{
		len = this->after.ListLength();
		cout << "The plans which is beginning in this month" << endl;
		for(i = 1; i <= len; i++)
		{
			temp = this->after.GetElem(i);
			if(temp.s_date.substr(5,2) == this->curTime.s_date.substr(5,2))	//如果该日程的开始月份等于当前日期就将它输出
				cout << temp;
		}
		cout << endl;
	}
	system("pause");
}

//析构函数
manage::~manage()
{
	fstream fp(this->db_fname.c_str(), ios::out);
	int len[3], i;
	agenda temp;

	len[0] = this->before.ListLength();
	len[1] = this->current.ListLength();
	len[2] = this->after.ListLength();
	fp << len[0]+len[1]+len[2] << endl;

	for(i = 1; i <= len[0]; i++)									//将所有日程写回日程文件
	{
		temp = ((this->before).GetElem(i));
		fp << temp;
	}
	for(i = 1; i <= len[1]; i++)
	{
		temp = ((this->current).GetElem(i));
		fp << temp;
	}
	for(i = 1; i <= len[2]; i++)
	{
		temp = ((this->after).GetElem(i));
		fp << temp;
	}
	fp.close();
}